package Model;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.transform.sax.SAXSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.XMLReaderFactory;

public class XMLvalidateXSD {
    public static boolean XMLvalidate(String xmlFilePatch, String xsdFilePatch){
        try {
            SchemaFactory sf=SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            File schemaFile=new File(xsdFilePatch);
            Schema xsdSchema=sf.newSchema(schemaFile);
            Validator validator=xsdSchema.newValidator();
            SAXSource source=new SAXSource(new InputSource(new FileInputStream(xmlFilePatch)));
            validator.validate(source,null);
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        }catch(SAXException ex){
            ex.printStackTrace();
            return false;
        }
        return true;
    }
}
